﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Hospital.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ImgController : ControllerBase
    {
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file">图片文件</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public IActionResult Imgs(IFormFile file)
        {
            try
            {
                //1、判断文件类型
                var t = file.ContentType;
                List<string> f = new List<string>
                {
                    "image/jpg",
                    "image/png",
                    "image/gif",
                    "image/jpeg",
                };
                if (!f.Contains(t)) 
                {
                    return Ok("不是图片");
                }
                //2、判断文件大小
                var max = 1024 * 1024 * 2;
                if (file.Length>max)
                {
                    return Ok("大于2M");
                }
                //3、上传图片
                string path=Directory.GetCurrentDirectory()+@"\wwwroot\Pic\"+file.FileName;
                using(FileStream fs=new FileStream(path,FileMode.Create))
                {
                    file.CopyTo(fs);
                    fs.Close();
                }
                return Ok("https://localhost:7018/Pic/" + file.FileName);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
